/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.imagelinkfixer;

import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import cz.mzk.kramerius.fedoraaccess.FedoraServer;
import cz.mzk.kramerius.fedoraaccess.rest.RESTConnector;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class Main {

    private static final int DELAY_MILLISECONDS = 100;//so that Fedora won't be overloaded
    private static final String KOMENSKY_FIXER = "KomenskyFixer";
    private static final String ATLAS_FIXER = "AtlasFixer";
    private static final String PDF_FIXER = "PdfFixer";
    private static final String USAGE = "Usage: java -jar KomenskyImageLinksFixer.jar \n"
            + "\t" + KOMENSKY_FIXER + "\t FEDORA_PROPERTIES_FILE TOP_LEVEL_PID \n"
            + "\t" + ATLAS_FIXER + "\t FEDORA_PROPERTIES_FILE FIXER_PROPERTIES_FILE MAPPING_FILE \n"
            + "\t" + PDF_FIXER + "\t FEDORA_PROPERTIES_FILE TOP_LEVEL_PID UUID_PDF_MAPPING_FILE \n";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //TODO: comment before commiting
//        args = new String[]{
//            ATLAS_FIXER,
//            "/home/rehan/secret/fedora.properties",
//            "/home/rehan/kramerius4/linkfixer/linkfixer.properties",
//            "/home/rehan/kramerius4/linkfixer/linkfixer.mapping"
//        };

//        args = new String[]{
//            PDF_FIXER,
//            "/home/rehan/secret/fedora.properties",
//            "/home/rehan/knizky_mlp/mapping.posledni"
//        };

        if (args.length == 0) {
            System.err.println("ERROR: no arguments presented");
            System.err.println(USAGE);
            return;
        }
        String fixer = args[0];
        if (KOMENSKY_FIXER.equals(fixer)) {
            runKomenskyFixer(withoutFirst(args));
        } else if (ATLAS_FIXER.equals(fixer)) {
            runAtlasFixer(withoutFirst(args));
        } else if (PDF_FIXER.equals(fixer)) {
            runPdfFixer(withoutFirst(args));
        } else {
            System.err.println("unkonwn fixer");
            System.err.println(USAGE);
        }
    }

    private static String[] withoutFirst(String[] args) {
        String[] result = new String[args.length - 1];
        for (int i = 1; i < args.length; i++) {
            result[i - 1] = args[i];
        }
        return result;
    }

    private static void runKomenskyFixer(String[] args) {
        try {
            if (args.length != 2) {
                System.err.println(buildIncorrectParametersError(2, args.length, KOMENSKY_FIXER));
                System.err.println(USAGE);
                return;
            }
            File properties = new File(args[0]);
            FedoraConnector conn = new RESTConnector(new FedoraServer(properties));
            String topLevelPid = args[1];
            KomenskyFixer fixer = new KomenskyFixer(conn, topLevelPid);
            fixer.run();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static String buildIncorrectParametersError(int expected, int found, String fixerName) {
        return "ERROR: incorrect numer of parameters for " + fixerName
                + " (found " + found + ", expected " + expected + ")";
    }

    private static void runAtlasFixer(String[] args) {
        try {
            if (args.length != 3) {
                System.err.println(buildIncorrectParametersError(3, args.length, ATLAS_FIXER));
                System.err.println(USAGE);
                return;
            }
            File fedoraProperties = new File(args[0]);
            FedoraConnector conn = new RESTConnector(new FedoraServer(fedoraProperties));
            File linkfixerProperties = new File(args[1]);
            File mappingFile = new File(args[2]);
            AtlasFixer fixer = new AtlasFixer(conn, linkfixerProperties, new PageFilenameMapping(mappingFile));
            fixer.run();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println(USAGE);
        }
    }

    /**
     *
     * @param fedoraPropertiesFile
     * @param mappingFile
     */
    private static void runPdfFixer(String[] args) {
        try {
            if (args.length != 2) {
                System.err.println(buildIncorrectParametersError(2, args.length, PDF_FIXER));
                System.err.println(USAGE);
                return;
            }
            File fedoraProperties = new File(args[0]);
            FedoraConnector conn = new RESTConnector(new FedoraServer(fedoraProperties));
            UuidPdfMapping mapping = new UuidPdfMapping(new File(args[1]));
            for (String uuid : mapping.getUuidSet()) {
                PdfFixer fixer = new PdfFixer("uuid:".concat(uuid), mapping.getPdfFile(uuid), conn);
                fixer.run();
                Thread.sleep(DELAY_MILLISECONDS);
            }
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
